#-*- coding: UTF-8 -*-
__author__ = 'Administrator'
import requests,os,sys,re
from bs4 import BeautifulSoup
from argparse import ArgumentParser
import prettytable

class ProxyValidator(object):
    validateUrl = 'http://ip.chinaz.com/'
    transparentProxies = []
    anonymousProxies = []
    uselessProxies = []

    def validateProxy(self,oproxy,type,timeout):
        '''
        validate single proxy
        '''
        print 'starting to validate ',oproxy
        proxy = type+r'://'+oproxy
        proxy = {type:proxy}
        try:
            req = requests.get(self.validateUrl,proxies=proxy,timeout=timeout)
            code = req.status_code
            content = req.content
            if code!=200:
                print 'connection error'
                self.uselessProxies.append(oproxy)
            else:
                print 'connection success'
                ipPattern = re.compile(r'(?<=</span>).*?(?=<span class="pl10")')
                result =  ipPattern.findall(content)
                if result[0].strip()==oproxy.split(':')[0].strip():
                    self.anonymousProxies.append(oproxy)
                else:
                    self.transparentProxies.append(oproxy)
        except:
            print 'connection error'
            self.uselessProxies.append(oproxy)
        anonymousProxy = prettytable.PrettyTable(["anonymouse proxies"])
        transparentProxy = prettytable.PrettyTable(["transparent proxies"])
        uselessProxy = prettytable.PrettyTable(['useless proxies'])
        for i in self.anonymousProxies:
            anonymousProxy.add_row([i])
        for k in self.transparentProxies:
            transparentProxy.add_row([k])
        for j in self.uselessProxies:
            uselessProxy.add_row([j])
        print anonymousProxy
        print transparentProxy
        print uselessProxy


    def validateProxyFile(self,file,timeout):
        '''
        validate proxies from a file
        one proxy per line
        row format as http://0.0.0.0:9999
        '''
        with open(file,'r') as f:
            lines = f.readlines()
            for ol in lines:
                protocol = ol.split(r'://')[0]
                addr = ol.split(r'://')[1]
                proxy = {protocol:ol.strip()}
                print 'starting to validate ',addr
                try:
                    req = requests.get(self.validateUrl,proxies=proxy,timeout=timeout)
                    code = req.status_code
                    content = req.content
                    if code!=200:
                        print 'connection error'
                        self.uselessProxies.append(ol)
                    else:
                        print 'connection success'
                        ipPattern = re.compile(r'(?<=</span>).*?(?=<span class="pl10")')
                        result =  ipPattern.findall(content)
                        if result[0].strip()==addr.split(':')[0].strip():
                            self.anonymousProxies.append(ol)
                        else:
                            self.transparentProxies.append(ol)
                except:
                    print 'connection error'
                    self.uselessProxies.append(ol)
        anonymousProxy = prettytable.PrettyTable(["anonymouse proxies"])
        transparentProxy = prettytable.PrettyTable(["transparent proxies"])
        uselessProxy = prettytable.PrettyTable(['useless proxies'])
        for i in self.anonymousProxies:
            anonymousProxy.add_row([i])
        for k in self.transparentProxies:
            transparentProxy.add_row([k])
        for j in self.uselessProxies:
            uselessProxy.add_row([j])
        print anonymousProxy
        print transparentProxy
        print uselessProxy


if __name__=='__main__':
    parser = ArgumentParser(description='proxy validate argument parser')
    parser.add_argument('-file',default=None,help='file containing proxy like http://0.0.0.0:1024')
    parser.add_argument('-proxy',default=None,help='single proxy,format as 0.0.0.0:0000')
    parser.add_argument('-type',default='http',help='proxy type,like https,http,default http')
    parser.add_argument('-timeout',default=3,help='proxy connection timeout,default 3')
    arg = parser.parse_args()
    validator = ProxyValidator()
    if arg.file is not None:
        f = arg.file
        if not os.path.isfile(f):
            print 'invalid proxy file , please try again'
            sys.exit()
        else:
            validator.validateProxyFile(f,arg.timeout)
    elif arg.proxy is not None:
        p = arg.proxy
        if p is None:
            print 'empty proxy argument,please try again'
            sys.exit()
        pattern = re.compile('(\d{1,3}\.){3}\d{1,3}:\d{2,4}')
        if pattern.match(p):
            validator.validateProxy(p.strip(),arg.type,arg.timeout)
        else:
            print 'invalid proxy format, please try again'
    else:
        print 'empty argument,try to input either a proxy or a proxy file'
        sys.exit()